home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Headers / STL Headers / function.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-06  |  7.5 KB  |  270 lines  |  [TEXT/MMCC]

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. #ifndef FUNCTION_H
  17. #define FUNCTION_H
  18.  
  19. #include <bool.h>
  20.  
  21. template <class T>
  22. inline bool operator!=(const T& x, const T& y) {
  23.     return !(x == y);
  24. }
  25.  
  26. template <class T>
  27. inline bool operator>(const T& x, const T& y) {
  28.     return y < x;
  29. }
  30.  
  31. template <class T>
  32. inline bool operator<=(const T& x, const T& y) {
  33.     return !(y < x);
  34. }
  35.  
  36. template <class T>
  37. inline bool operator>=(const T& x, const T& y) {
  38.     return !(x < y);
  39. }
  40.  
  41. template <class Arg, class Result>
  42. struct unary_function {
  43.     typedef Arg argument_type;
  44.     typedef Result result_type;
  45. };
  46.  
  47. template <class Arg1, class Arg2, class Result>
  48. struct binary_function {
  49.     typedef Arg1 first_argument_type;
  50.     typedef Arg2 second_argument_type;
  51.     typedef Result result_type;
  52. };      
  53.  
  54. template <class T>
  55. struct plus : binary_function<T, T, T> {
  56.     T operator()(const T& x, const T& y) const { return x + y; }
  57. };
  58.  
  59. template <class T>
  60. struct minus : binary_function<T, T, T> {
  61.     T operator()(const T& x, const T& y) const { return x - y; }
  62. };
  63.  
  64. template <class T>
  65. struct times : binary_function<T, T, T> {
  66.     T operator()(const T& x, const T& y) const { return x * y; }
  67. };
  68.  
  69. template <class T>
  70. struct divides : binary_function<T, T, T> {
  71.     T operator()(const T& x, const T& y) const { return x / y; }
  72. };
  73.  
  74. template <class T>
  75. struct modulus : binary_function<T, T, T> {
  76.     T operator()(const T& x, const T& y) const { return x % y; }
  77. };
  78.  
  79. template <class T>
  80. struct negate : unary_function<T, T> {
  81.     T operator()(const T& x) const { return -x; }
  82. };
  83.  
  84. template <class T>
  85. struct equal_to : binary_function<T, T, bool> {
  86.     bool operator()(const T& x, const T& y) const { return x == y; }
  87. };
  88.  
  89. template <class T>
  90. struct not_equal_to : binary_function<T, T, bool> {
  91.     bool operator()(const T& x, const T& y) const { return x != y; }
  92. };
  93.  
  94. template <class T>
  95. struct greater : binary_function<T, T, bool> {
  96.     bool operator()(const T& x, const T& y) const { return x > y; }
  97. };
  98.  
  99. template <class T>
  100. struct less : binary_function<T, T, bool> {
  101.     bool operator()(const T& x, const T& y) const { return x < y; }
  102. };
  103.  
  104. template <class T>
  105. struct greater_equal : binary_function<T, T, bool> {
  106.     bool operator()(const T& x, const T& y) const { return x >= y; }
  107. };
  108.  
  109. template <class T>
  110. struct less_equal : binary_function<T, T, bool> {
  111.     bool operator()(const T& x, const T& y) const { return x <= y; }
  112. };
  113.  
  114. template <class T>
  115. struct logical_and : binary_function<T, T, bool> {
  116.     bool operator()(const T& x, const T& y) const { return x && y; }
  117. };
  118.  
  119. template <class T>
  120. struct logical_or : binary_function<T, T, bool> {
  121.     bool operator()(const T& x, const T& y) const { return x || y; }
  122. };
  123.  
  124. template <class T>
  125. struct logical_not : unary_function<T, bool> {
  126.     bool operator()(const T& x) const { return !x; }
  127. };
  128.  
  129. template <class Predicate>
  130. class unary_negate : public unary_function<Predicate::argument_type, bool> {
  131. protected:
  132.     Predicate pred;
  133. public:
  134.     unary_negate(const Predicate& x) : pred(x) {}
  135.     bool operator()(const argument_type& x) const { return !pred(x); }
  136. };
  137.  
  138. template <class Predicate>
  139. unary_negate<Predicate> not1(const Predicate& pred) {
  140.     return unary_negate<Predicate>(pred);
  141. }
  142.  
  143. template <class Predicate> 
  144. class binary_negate 
  145.     : public binary_function<Predicate::first_argument_type,
  146.                  Predicate::second_argument_type, bool> {
  147. protected:
  148.     Predicate pred;
  149. public:
  150.     binary_negate(const Predicate& x) : pred(x) {}
  151.     bool operator()(const first_argument_type& x, 
  152.             const second_argument_type& y) const {
  153.     return !pred(x, y); 
  154.     }
  155. };
  156.  
  157. template <class Predicate>
  158. binary_negate<Predicate> not2(const Predicate& pred) {
  159.     return binary_negate<Predicate>(pred);
  160. }
  161.  
  162. template <class Operation> 
  163. class binder1st : public unary_function<Operation::second_argument_type,
  164.                     Operation::result_type> {
  165. protected:
  166.     Operation op;
  167.     Operation::first_argument_type value;
  168. public:
  169.     binder1st(const Operation& x, const Operation::first_argument_type& y)
  170.     : op(x), value(y) {}
  171.     result_type operator()(const argument_type& x) const {
  172.     return op(value, x); 
  173.     }
  174. };
  175.  
  176. template <class Operation, class T>
  177. binder1st<Operation> bind1st(const Operation& op, const T& x) {
  178.     return binder1st<Operation>(op, Operation::first_argument_type(x));
  179. }
  180.  
  181. template <class Operation> 
  182. class binder2nd : public unary_function<Operation::first_argument_type,
  183.                     Operation::result_type> {
  184. protected:
  185.     Operation op;
  186.     Operation::second_argument_type value;
  187. public:
  188.     binder2nd(const Operation& x, const Operation::second_argument_type& y) 
  189.     : op(x), value(y) {}
  190.     result_type operator()(const argument_type& x) const {
  191.     return op(x, value); 
  192.     }
  193. };
  194.  
  195. template <class Operation, class T>
  196. binder2nd<Operation> bind2nd(const Operation& op, const T& x) {
  197.     return binder2nd<Operation>(op, Operation::second_argument_type(x));
  198. }
  199.  
  200. template <class Operation1, class Operation2>
  201. class unary_compose : public unary_function<Operation2::argument_type,
  202.                                             Operation1::result_type> {
  203. protected:
  204.     Operation1 op1;
  205.     Operation2 op2;
  206. public:
  207.     unary_compose(const Operation1& x, const Operation2& y) : op1(x), op2(y) {}
  208.     result_type operator()(const argument_type& x) const {
  209.     return op1(op2(x));
  210.     }
  211. };
  212.  
  213. template <class Operation1, class Operation2>
  214. unary_compose<Operation1, Operation2> compose1(const Operation1& op1, 
  215.                            const Operation2& op2) {
  216.     return unary_compose<Operation1, Operation2>(op1, op2);
  217. }
  218.  
  219. template <class Operation1, class Operation2, class Operation3>
  220. class binary_compose : public unary_function<Operation2::argument_type,
  221.                                              Operation1::result_type> {
  222. protected:
  223.     Operation1 op1;
  224.     Operation2 op2;
  225.     Operation3 op3;
  226. public:
  227.     binary_compose(const Operation1& x, const Operation2& y, 
  228.            const Operation3& z) : op1(x), op2(y), op3(z) { }
  229.     result_type operator()(const argument_type& x) const {
  230.     return op1(op2(x), op3(x));
  231.     }
  232. };
  233.  
  234. template <class Operation1, class Operation2, class Operation3>
  235. binary_compose<Operation1, Operation2, Operation3> 
  236. compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3) {
  237.     return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);
  238. }
  239.  
  240. template <class Arg, class Result>
  241. class pointer_to_unary_function : public unary_function<Arg, Result> {
  242. protected:
  243.     Result (*ptr)(Arg);
  244. public:
  245.     pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}
  246.     Result operator()(Arg x) const { return ptr(x); }
  247. };
  248.  
  249. template <class Arg, class Result>
  250. pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg)) {
  251.     return pointer_to_unary_function<Arg, Result>(x);
  252. }
  253.  
  254. template <class Arg1, class Arg2, class Result>
  255. class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> {
  256. protected:
  257.     Result (*ptr)(Arg1, Arg2);
  258. public:
  259.     pointer_to_binary_function(Result (*x)(Arg1, Arg2)) : ptr(x) {}
  260.     Result operator()(Arg1 x, Arg2 y) const { return ptr(x, y); }
  261. };
  262.  
  263. template <class Arg1, class Arg2, class Result>
  264. pointer_to_binary_function<Arg1, Arg2, Result> 
  265. ptr_fun(Result (*x)(Arg1, Arg2)) {
  266.     return pointer_to_binary_function<Arg1, Arg2, Result>(x);
  267. }
  268.  
  269. #endif
  270.